package com.me.tags;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
import java.io.StringWriter;
import java.lang.reflect.InvocationTargetException;

public class OutTag extends SimpleTagSupport {
	private String jsp_for;

	@Override
	public void doTag() throws JspException, IOException {
		PageContext page = ((PageContext) this.getJspContext());
		JspWriter out = page.getOut();
		Object obj = page.getRequest().getAttribute("Model");

		try {
			out.print(
					obj.getClass().
							getMethod("get" + jsp_for.substring(0,1).toUpperCase() + jsp_for.substring(1)).
							invoke(obj).toString()
			);
		} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
			e.printStackTrace();
		}
	}

	public void setJsp_for(String jsp_for) {
		this.jsp_for = jsp_for;
	}
}
